package cn.edu.zjut.mina.xcmahjong;

import java.util.ArrayList;
import java.util.Currency;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.apache.mina.core.session.IoSession;

import com.google.gson.Gson;

import cn.edu.zjut.mina.bean.PlayerInfo;
import cn.edu.zjut.mina.bean.StandardFormat;
import cn.edu.zjut.mina.interf.ICardBean;
import cn.edu.zjut.mina.interf.IGame;
import cn.edu.zjut.mina.interf.IPlayer;
import cn.edu.zjut.mina.protocal.JMessageProtocalRes;
import cn.edu.zjut.mina.protocal.ProtocalRegulation;
import cn.edu.zjut.mina.util.GsonUtils;

public class XcmjPlayer implements IPlayer {

	private XcmjPGArea pgArea = new XcmjPGArea();
	// private Scanner sc = new Scanner(System.in);
	private List<ArrayList<Integer>> hand = new ArrayList<ArrayList<Integer>>();
	// 手牌
	private List<ArrayList<ICardBean>> handCards = new ArrayList<ArrayList<ICardBean>>();

	public List<ArrayList<ICardBean>> getHandCards() {
		return handCards;
	}

	public void setHandCards(List<ArrayList<ICardBean>> handCards) {
		this.handCards = handCards;
	}

	public List<ICardBean> getSendCards() {
		return sendCards;
	}

	public void setSendCards(List<ICardBean> sendCards) {
		this.sendCards = sendCards;
	}

	// 弃牌
	private List<ICardBean> sendCards = new ArrayList<ICardBean>();

	private List<ICardBean> cards = new ArrayList<ICardBean>();
	private boolean banker = false;
	private XcmjGame xcmjGame = null;
	//用户Id
	private String userId;
	//用户session
	private IoSession session;
	//自摸次数
	private int zimoTimes;
	//胡次数
	private int huTimes;
	//明杠次数
	private int mingGangTimes;
	//暗杠次数
	private int anGangTimes;
	// number 玩家编号
	private int position;
	//接炮次数
	private int jiePaoTimes;
	//点炮次数
	private int dianPaoTimes;
	//类型数
	private int typeNum;

	public void init() {
		//初始化统计次数
		zimoTimes = huTimes = mingGangTimes = anGangTimes = 0;
		this.jiePaoTimes = 0;
		this.dianPaoTimes = 0;
		
		pgArea.clear();
		banker = false;
		xcmjGame = null;

		cards.clear();
		hand.clear();
		handCards.clear();
		sendCards.clear();

		handCards.add(new ArrayList<ICardBean>());// 万
		handCards.add(new ArrayList<ICardBean>());// 筒
		handCards.add(new ArrayList<ICardBean>());// 条
		handCards.add(new ArrayList<ICardBean>());// 字

		hand.add(new ArrayList<Integer>());// c
		hand.add(new ArrayList<Integer>());// i
		hand.add(new ArrayList<Integer>());// o
		hand.add(new ArrayList<Integer>());// w
	}

	public XcmjPGArea getPgArea() {
		return pgArea;
	}

	public List<ICardBean> getCards() {
		return cards;
	}


	public void setTypeNum() {
		typeNum = 0;
		boolean haveWan = false;
		boolean haveTong = false;
		boolean haveTiao = false;

		if (handCards.get(0).size() != 0)
			haveWan = true;
		if (handCards.get(1).size() != 0)
			haveTong = true;
		if (handCards.get(2).size() != 0)
			haveTiao = true;
		for (int i = 0; i < sendCards.size(); i++) {
			if (sendCards.get(i).getTypeInt() == 1)
				haveWan = true;
			if (sendCards.get(i).getTypeInt() == 2)
				haveTong = true;
			if (sendCards.get(i).getTypeInt() == 3)
				haveTiao = true;
		}
		if (haveWan)
			typeNum++;
		if (haveTong)
			typeNum++;
		if (haveTiao)
			typeNum++;
	}

	public void setGame(IGame xcmjGame) {
		this.xcmjGame = (XcmjGame) xcmjGame;
	}

	//碰
	public void doPeng(StandardFormat inJson, IoSession session) {
		List<XcmjCardBean> sendCardBeans = new ArrayList<XcmjCardBean>();// 发给其他玩家的牌
		int type = inJson.cardType;
		int number = inJson.cardNumber;
		//保存了麻将的index，前两位数为手牌index，个位数为他人的牌的index
		int indexs = inJson.cardIndexs;
		int index1 = indexs / 100;
		int index2 = (indexs / 10) % 10;
		int index3 = indexs % 10;
		// 三张牌
		XcmjCardBean pengCardBean1 = new XcmjCardBean();
		XcmjCardBean pengCardBean2 = new XcmjCardBean();
		XcmjCardBean pengCardBean3 = new XcmjCardBean();

		pengCardBean1.setTypeInt((int) inJson.cardType);
		pengCardBean1.setNumber((int) inJson.cardNumber);
		pengCardBean1.setName();

		pengCardBean2.setTypeInt((int) inJson.cardType);
		pengCardBean2.setNumber((int) inJson.cardNumber);
		pengCardBean2.setName();

		pengCardBean3.setTypeInt((int) inJson.cardType);
		pengCardBean3.setNumber((int) inJson.cardNumber);
		pengCardBean3.setName();

		// 添加第一张牌
		pengCardBean1.setIndex(index1);
		pengCardBean1.setUserId((String) inJson.userId);
		System.out.println("第一张牌" + pengCardBean1.toString());
		sendCardBeans.add(pengCardBean1);
		pgArea.addPengCard(pengCardBean1);
		// 添加第二张牌
		pengCardBean2.setUserId((String) inJson.userId);
		pengCardBean2.setIndex(index2);
		System.out.println("第二张牌" + pengCardBean2.toString());
		sendCardBeans.add(pengCardBean2);
		pgArea.addPengCard(pengCardBean2);
		// 设置第三张牌index
		pengCardBean3.setIndex(index3);
		// 遍历4个人，取出该玩家弃牌堆里的碰牌
		for (int i = 0; i < 4; i++) {
			XcmjPlayer player = xcmjGame.players[i];
			// 是自己则继续遍历
			if (player.getUserId().equals(this.userId))
				continue;
			boolean haveFindPengCard = false;
			for (int j = 0; j < player.getSendCards().size(); j++) {
				XcmjCardBean pCardBean = (XcmjCardBean) player.getSendCards().get(j);
				if (pCardBean.getTypeInt() == inJson.cardType && pCardBean.getNumber() == inJson.cardNumber
						&& pCardBean.getIndex() == index3) {
					player.getSendCards().remove(j);
					// 设置第三张牌的userId
					pengCardBean3.setUserId(player.getUserId());
					haveFindPengCard = true;
					System.out.println("删除第三张牌" + pCardBean.getIndex());
					break;
				}
			}
			if (haveFindPengCard)
				break;
		}
		// 添加第三张牌
		System.out.println("第三张牌" + pengCardBean3.toString());
		sendCardBeans.add(pengCardBean3);
		pgArea.addPengCard(pengCardBean3);
		for (int i = 0; i < sendCardBeans.size(); i++) {
			System.out.println(sendCardBeans.get(i).toString());
		}

		// 删除手牌
		// ArrayList<ICardBean> myCardBeans = handCards.get(type - 1);
		Iterator<ICardBean> it = handCards.get(type - 1).iterator();

		while (it.hasNext()) {
			ICardBean cardBean = it.next();
			// 删除第一张
			if (cardBean.getTypeInt() == inJson.cardType && cardBean.getNumber() == number
					&& cardBean.getIndex() == index1) {
				it.remove();
			}
			// 删除第二张
			if (cardBean.getTypeInt() == inJson.cardType && cardBean.getNumber() == number
					&& cardBean.getIndex() == index2) {
				it.remove();
			}
		}

		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result = true;
		outJson.cardBeans = sendCardBeans;
		outJson.oper = this.position;
		outJson.userId = userId;
		outJson.operation = 0;

		res.setResultCode(ProtocalRegulation.OTHEROPERATION);
		String data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		xcmjGame.sendToOthers(res, this.position);

		res = new JMessageProtocalRes();
		outJson = new StandardFormat();
		outJson.result = true;
		outJson.resultInfo = "碰成功";

		res.setResultCode(ProtocalRegulation.PENGSUCCESS);
		data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		session.write(res);
	}

	// 自摸暗杠
	public void doZimoAnGang(StandardFormat inJson, IoSession session) {
		System.out.println("doZimoAnGang自摸暗杠");
		// 自摸暗杠只需要type和number
		int type = inJson.cardType;
		int number = inJson.cardNumber;
		System.out.println("" + type + number);

		XcmjCardBean gangCardBean1 = new XcmjCardBean();
		gangCardBean1.setTypeInt((int) inJson.cardType);
		gangCardBean1.setNumber((int) inJson.cardNumber);
		gangCardBean1.setName();
		gangCardBean1.setIndex(1);
		gangCardBean1.setUserId(inJson.userId);

		XcmjCardBean gangCardBean2 = new XcmjCardBean();
		gangCardBean2.setTypeInt((int) inJson.cardType);
		gangCardBean2.setNumber((int) inJson.cardNumber);
		gangCardBean2.setName();
		gangCardBean2.setIndex(2);
		gangCardBean2.setUserId(inJson.userId);

		XcmjCardBean gangCardBean3 = new XcmjCardBean();
		gangCardBean3.setTypeInt((int) inJson.cardType);
		gangCardBean3.setNumber((int) inJson.cardNumber);
		gangCardBean3.setName();
		gangCardBean3.setIndex(3);
		gangCardBean3.setUserId(inJson.userId);

		XcmjCardBean gangCardBean4 = new XcmjCardBean();
		gangCardBean4.setTypeInt((int) inJson.cardType);
		gangCardBean4.setNumber((int) inJson.cardNumber);
		gangCardBean4.setName();
		gangCardBean4.setIndex(4);
		gangCardBean4.setUserId(inJson.userId);
		// 添加到杠区
		pgArea.addGangCard(gangCardBean1);
		pgArea.addGangCard(gangCardBean2);
		pgArea.addGangCard(gangCardBean3);
		pgArea.addGangCard(gangCardBean4);
		
		//删除4张手牌
		Iterator<ICardBean> it = handCards.get(type - 1).iterator();
		while (it.hasNext()) {
			ICardBean cardBean = it.next();
			if (cardBean.getTypeInt() == type && cardBean.getNumber() == number) {
				it.remove();
			}
		}
		
		//响应其他用户
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result = true;
		outJson.cardBean = gangCardBean1;
		outJson.oper = this.position;
		outJson.userId = userId;
		outJson.gangType = 3;// 自摸暗杠
		outJson.operation = 1;

		res.setResultCode(ProtocalRegulation.OTHEROPERATION);
		String data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		xcmjGame.sendToOthers(res, this.position);
		
		//响应暗杠用户
		res = new JMessageProtocalRes();
		outJson = new StandardFormat();
		outJson.result = true;
		outJson.resultInfo = "自摸暗杠成功";

		res.setResultCode(ProtocalRegulation.ZIMOANGANGSUCCESS);
		data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		session.write(res);
		//杠了之后摸牌
		getCard();
	}

	// 自摸明杠
	public void doZimoMingGang(StandardFormat inJson, IoSession session) {
		int type = inJson.cardType;
		int number = inJson.cardNumber;
		// 原来手上的3张牌的idnex
		int index = inJson.cardIndex;

		XcmjCardBean gangCardBean = new XcmjCardBean();

		gangCardBean.setTypeInt((int) inJson.cardType);
		gangCardBean.setNumber((int) inJson.cardNumber);
		gangCardBean.setName();
		gangCardBean.setIndex(index);
		gangCardBean.setUserId(inJson.userId);

		pgArea.addGangCard(gangCardBean);

		Iterator<ICardBean> it = handCards.get(type - 1).iterator();
		while (it.hasNext()) {
			ICardBean cardBean = it.next();
			// 删除第一张
			if (cardBean.getTypeInt() == inJson.cardType && cardBean.getNumber() == number
					&& cardBean.getIndex() == index) {
				it.remove();
			}
		}

		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result = true;
		outJson.cardBean = gangCardBean;
		outJson.oper = this.position;
		outJson.userId = userId;
		outJson.gangType = 2;
		outJson.operation = 1;

		res.setResultCode(ProtocalRegulation.OTHEROPERATION);
		String data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		xcmjGame.sendToOthers(res, this.position);

		res = new JMessageProtocalRes();
		outJson = new StandardFormat();
		outJson.result = true;
		outJson.resultInfo = "自摸明杠成功";

		res.setResultCode(ProtocalRegulation.ZIMOMINGGANGSUCCESS);
		data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		session.write(res);

		getCard();
	}

	// 别人打过来杠
	public void doGang(StandardFormat inJson, IoSession session) {
		List<XcmjCardBean> sendCardBeans = new ArrayList<XcmjCardBean>();// 发给其他玩家的牌
		int type = inJson.cardType;
		int number = inJson.cardNumber;
		// 原来手上的3张牌的idnex
		int indexs = inJson.cardIndexs;
		int index1 = indexs / 100;
		int index2 = (indexs / 10) % 10;
		int index3 = indexs % 10;

		// 4张杠牌
		XcmjCardBean gangCardBean1 = new XcmjCardBean();
		XcmjCardBean gangCardBean2 = new XcmjCardBean();
		XcmjCardBean gangCardBean3 = new XcmjCardBean();
		XcmjCardBean gangCardBean4 = new XcmjCardBean();

		gangCardBean1.setTypeInt((int) inJson.cardType);
		gangCardBean1.setNumber((int) inJson.cardNumber);
		gangCardBean1.setName();
		gangCardBean1.setIndex(index1);
		gangCardBean1.setUserId(inJson.userId);
		sendCardBeans.add(gangCardBean1);

		gangCardBean2.setTypeInt((int) inJson.cardType);
		gangCardBean2.setNumber((int) inJson.cardNumber);
		gangCardBean2.setName();
		gangCardBean2.setIndex(index2);
		gangCardBean2.setUserId(inJson.userId);
		sendCardBeans.add(gangCardBean2);

		gangCardBean3.setTypeInt((int) inJson.cardType);
		gangCardBean3.setNumber((int) inJson.cardNumber);
		gangCardBean3.setName();
		gangCardBean3.setIndex(index3);
		gangCardBean3.setUserId(inJson.userId);
		sendCardBeans.add(gangCardBean3);

		gangCardBean4.setTypeInt((int) inJson.cardType);
		gangCardBean4.setNumber((int) inJson.cardNumber);
		gangCardBean4.setName();
		// 先遍历牌堆，删除其他玩家弃牌堆里的牌
		// 遍历4个人，取出该玩家弃牌堆里的碰牌
		for (int i = 0; i < 4; i++) {
			XcmjPlayer player = xcmjGame.players[i];
			// 是自己则继续遍历
			if (player.getUserId().equals(this.userId))
				continue;
			boolean haveFindGangCard = false;
			for (int j = 0; j < player.getSendCards().size(); j++) {
				XcmjCardBean pCardBean = (XcmjCardBean) player.getSendCards().get(j);
				if (pCardBean.getTypeInt() == inJson.cardType && pCardBean.getNumber() == inJson.cardNumber) {
					player.getSendCards().remove(j);
					// 设置第四张牌的userId,index
					gangCardBean4.setUserId(player.getUserId());
					gangCardBean4.setIndex(pCardBean.getIndex());
					haveFindGangCard = true;
					break;
				}
			}
			if (haveFindGangCard)
				break;
		}
		sendCardBeans.add(gangCardBean4);
		pgArea.addGangCard(gangCardBean1);
		pgArea.addGangCard(gangCardBean2);
		pgArea.addGangCard(gangCardBean3);
		pgArea.addGangCard(gangCardBean4);

		Iterator<ICardBean> it = handCards.get(type - 1).iterator();
		while (it.hasNext()) {
			ICardBean cardBean = it.next();
			// 删除第一张
			if (cardBean.getTypeInt() == inJson.cardType && cardBean.getNumber() == number
					&& cardBean.getIndex() == index1) {
				it.remove();
			}
			// 删除第二张
			if (cardBean.getTypeInt() == inJson.cardType && cardBean.getNumber() == number
					&& cardBean.getIndex() == index2) {
				it.remove();
			}
			// 删除第二张
			if (cardBean.getTypeInt() == inJson.cardType && cardBean.getNumber() == number
					&& cardBean.getIndex() == index3) {
				it.remove();
			}
		}

		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result = true;
		outJson.cardBeans = sendCardBeans;
		outJson.oper = this.position;
		outJson.userId = userId;
		outJson.operation = 1;
		outJson.gangType = 1;

		res.setResultCode(ProtocalRegulation.OTHEROPERATION);
		String data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		xcmjGame.sendToOthers(res, this.position);

		res = new JMessageProtocalRes();
		outJson = new StandardFormat();
		outJson.result = true;
		outJson.resultInfo = "杠成功";

		res.setResultCode(ProtocalRegulation.SENDGANGSUCCESS);
		data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		session.write(res);

		getCard();
	}

	public void doHu(StandardFormat inJson, IoSession session) {
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		PlayerInfo playinfo = new PlayerInfo();
		huTimes++;
		if (xcmjGame.currentPlayer != position) {
			// 点炮
			huScore = 1;
			putDp();
			xcmjGame.players[xcmjGame.currentPlayer].putJp();
		} else {
			huScore = 4;
			zimoTimes++;
		}

		setTypeNum();
		switch (typeNum) {
		case 2:
			typeScore = 1;
			break;
		case 1:
			if (hasW) {
				typeScore = 2;
			} else {
				typeScore = 3;
			}
			break;
		case 0:
			typeScore = 5;
			break;

		default:
			break;
		}
		currentScore = mingGangTimes + anGangTimes * 4 + huScore * typeScore;
		playinfo.userId = userId;
		playinfo.position = position;
		playinfo.huTimes = huTimes;
		playinfo.bareGang = mingGangTimes;
		playinfo.secretGang = anGangTimes;
		playinfo.zimoTimes = zimoTimes;
		playinfo.score = currentScore;
		playinfo.dianPao = dianPao;
		playinfo.jiePao = jiePao;

		outJson.players.add(playinfo);

		for (int i = 1; i < 4; i++) {
			playinfo = new PlayerInfo();
			playinfo.userId = xcmjGame.players[(number + i) % 4].userId;
			playinfo.position = xcmjGame.players[(number + i) % 4].number;
			playinfo.huTimes = xcmjGame.players[(number + i) % 4].huTimes;

			playinfo.bareGang = xcmjGame.players[(number + i) % 4].mingGangTimes;
			playinfo.secretGang = xcmjGame.players[(number + i) % 4].anGangTimes;
			playinfo.zimoTimes = xcmjGame.players[(number + i) % 4].zimoTimes;

			playinfo.score = xcmjGame.players[(number + i) % 4].mingGangTimes + xcmjGame.players[(number + i) % 4].anGangTimes * 4;

			playinfo.dianPao = xcmjGame.players[(number + i) % 4].dianPao;
			playinfo.jiePao = xcmjGame.players[(number + i) % 4].jiePao;

			outJson.players.add(playinfo);
		}
		int ts = 0;// 需要减去的分
		for (int i = 0; i < 4; i++) {
			ts += outJson.players.get(i).score;
		}
		// 点炮
		if (huScore == 1) {
			ts -= currentScore;
			for (int i = 0; i < 4; i++) {
				if (outJson.players.get(i).position == xcmjGame.currentPlayer) {
					outJson.players.get(i).totalScore -= 3 * currentScore;
					xcmjGame.players[xcmjGame.currentPlayer].totalScore -= 3 * currentScore;
					outJson.players.get(0).totalScore -= currentScore;
					totalScore -= currentScore;
				}
			}
		}
		for (int i = 0; i < 4; i++) {
			outJson.players.get(i).totalScore = outJson.players.get(i).totalScore - ts
					+ 4 * outJson.players.get(i).score;
			xcmjGame.players[outJson.players.get(i).position].totalScore = outJson.players.get(i).totalScore - ts
					+ 4 * outJson.players.get(i).score;
		}
		outJson.result = true;
		String data = GsonUtils.beanToJson(outJson);
		res.setResultCode(ProtocalRegulation.STATISTICSCORE);
		res.setContent(data);

		xcmjGame.sendToEveryone(res);
	}
	
	// 添加手牌
	public void addCard(XcmjCardBean cardBean) {
		int type = cardBean.getTypeInt();

		int num = cardBean.getNumber();
		cards.add(cardBean);
		// System.out.println("addCard:type=" + type + " num=" + num);
		switch (type) {
		case 1:
			handCards.get(0).add(cardBean);
			hand.get(0).add(num);
			hand.get(0).sort(new XcmjCardComparator());
			break;
		case 2:
			handCards.get(1).add(cardBean);
			hand.get(1).add(num);
			hand.get(1).sort(new XcmjCardComparator());
			break;
		case 3:
			handCards.get(2).add(cardBean);
			hand.get(2).add(num);
			hand.get(2).sort(new XcmjCardComparator());
			break;
		case 4:
			handCards.get(3).add(cardBean);
			hand.get(3).add(num);
			hand.get(3).sort(new XcmjCardComparator());
			break;
		default:
			break;
		}
	}

	// 删除玩家手牌
	public void removeCard(XcmjCardBean cardBean) {

		int index = cardBean.getIndex();
		int number = cardBean.getNumber();
		int type = cardBean.getTypeInt();

		// 以下为删除手牌操作，判断type、number、index 相同则删除
		for (int i = 0; i < cards.size(); i++) {
			if (cards.get(i).getIndex() == index && cards.get(i).getNumber() == number
					&& cards.get(i).getTypeInt() == type) {
				cards.remove(i);
				break;
			}
		}

		switch (cardBean.getTypeInt()) {
		case 1:
			for (int i = 0; i < handCards.get(0).size(); i++) {
				if (handCards.get(0).get(i).getIndex() == index && handCards.get(0).get(i).getNumber() == number
						&& handCards.get(0).get(i).getTypeInt() == type) {
					handCards.get(0).remove(i);
					break;
				}
			}
			break;
		case 2:
			for (int i = 0; i < handCards.get(1).size(); i++) {
				if (handCards.get(1).get(i).getIndex() == index && handCards.get(1).get(i).getNumber() == number
						&& handCards.get(1).get(i).getTypeInt() == type) {
					handCards.get(1).remove(i);
					break;
				}
			}
			break;
		case 3:
			for (int i = 0; i < handCards.get(2).size(); i++) {
				if (handCards.get(2).get(i).getIndex() == index && handCards.get(2).get(i).getNumber() == number
						&& handCards.get(2).get(i).getTypeInt() == type) {
					handCards.get(2).remove(i);
					break;
				}
			}
			break;
		case 4:
			for (int i = 0; i < handCards.get(3).size(); i++) {
				if (handCards.get(3).get(i).getIndex() == index && handCards.get(3).get(i).getNumber() == number
						&& handCards.get(3).get(i).getTypeInt() == type) {
					handCards.get(3).remove(i);
					break;
				}
			}
			break;
		default:
			break;
		}

	}

	// 鍒ゆ柇鏈夋棤aaa鎴朼bc
	public static boolean isThree(ArrayList<Integer> cards) {
		ArrayList<Integer> temp = new ArrayList<Integer>();
		temp.addAll(cards);
		temp.sort(new XcmjCardComparator());
		int[] pile = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		for (int i = 0; i < temp.size(); i++) {
			pile[temp.get(i) - 1]++;
		}
		int noz = 0;// 0的数量

		// 无视3个一样的时候
		for (int j = 0; j < 2; j++) {
			for (int i = 0; i < 9; i++) {
				if ((pile[i] > 0 && pile[i] < 3) || pile[i] == 4)
					if ((pile[i + 1] > 0 && pile[i + 1] < 3) || pile[i + 1] == 4)
						if ((pile[i + 2] > 0 && pile[i + 2] < 3) || pile[i + 2] == 4) {
							pile[i]--;
							pile[i + 1]--;
							pile[i + 2]--;
						}
			}
		}

		for (int j = 0; j < 3; j++) {
			for (int i = 0; i < 9; i++) {
				if (pile[i] > 0)
					if (pile[i + 1] > 0)
						if (pile[i + 2] > 0) {
							pile[i]--;
							pile[i + 1]--;
							pile[i + 2]--;
						}
			}
		}

		// 若全为0或者3则胡
		noz = 0;
		for (int z = 0; z < 9; z++)
			if (pile[z] == 0 || pile[z] == 3)
				noz++;
		if (noz != 9) {
			System.out.println("false");
			return false;
		}
		System.out.println("true");
		return true;
	}

	// 字用的isThree
	public boolean isThreeW(ArrayList<Integer> cards) {
		for (int i = 0; i < cards.size() - 2; i++) {
			if (cards.get(i) == cards.get(i + 1)) {
				if (cards.get(i + 1) == cards.get(i + 2)) {
					i += 2;
					continue;
				} else
					return false;
			} else if (cards.get(i) == cards.get(i + 1) - 1) {
				if (cards.get(i + 1) == cards.get(i + 2) - 1) {
					i += 2;
					continue;
				} else
					return false;
			} else {
				return false;
			}
		}
		return true;
	}

	public boolean isHu(XcmjCardBean cb) {
		int dbCard = 0;
		if (cb == null) {
			setTypeNum();
			if (typeNum == 3) {
				System.out.println("玩家" + getPosition() + "花色数为" + typeNum + "   尚未满足条件");
				return false;
			}
			System.out.println("玩家" + getPosition() + "花色数为" + typeNum + "   满足第一个条件");
			// ciow
			if ((hand.get(0).size() - 2) % 3 == 0) {
				if (hand.get(1).size() % 3 == 0 && hand.get(2).size() % 3 == 0 && hand.get(3).size() % 3 == 0) {
					for (int i = 0; i < hand.get(0).size() - 1; i++) {
						if (hand.get(0).get(i) == hand.get(0).get(i + 1)) {
							dbCard = hand.get(0).get(i);
							hand.get(0).remove((Integer) dbCard);
							hand.get(0).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(0).add(dbCard);
								hand.get(0).add(dbCard);
								hand.get(0).sort(new XcmjCardComparator());
								return true;
							} else {
								hand.get(0).add(dbCard);
								hand.get(0).add(dbCard);
								hand.get(0).sort(new XcmjCardComparator());
								continue;
							}
						}
					}
				} else {
					return false;
				}
			} else if ((hand.get(1).size() - 2) % 3 == 1) {
				if (hand.get(0).size() % 3 == 0 && hand.get(2).size() % 3 == 0 && hand.get(3).size() % 3 == 0) {
					for (int i = 0; i < hand.get(1).size() - 1; i++) {
						if (hand.get(1).get(i) == hand.get(1).get(i + 1)) {
							dbCard = hand.get(1).get(i);
							hand.get(1).remove((Integer) dbCard);
							hand.get(1).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(1).add(dbCard);
								hand.get(1).add(dbCard);
								hand.get(1).sort(new XcmjCardComparator());
								return true;
							} else {
								hand.get(1).add(dbCard);
								hand.get(1).add(dbCard);
								hand.get(1).sort(new XcmjCardComparator());
								continue;
							}
						}
					}

				} else {
					return false;
				}
			} else if ((hand.get(2).size() - 2) % 3 == 0) {
				if (hand.get(1).size() % 3 == 0 && hand.get(0).size() % 3 == 0 && hand.get(3).size() % 3 == 0) {
					for (int i = 0; i < hand.get(2).size() - 1; i++) {
						if (hand.get(2).get(i) == hand.get(2).get(i + 1)) {
							dbCard = hand.get(2).get(i);
							hand.get(2).remove((Integer) dbCard);
							hand.get(2).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(2).add(dbCard);
								hand.get(2).add(dbCard);
								hand.get(2).sort(new XcmjCardComparator());
								return true;
							} else {
								hand.get(2).add(dbCard);
								hand.get(2).add(dbCard);
								hand.get(2).sort(new XcmjCardComparator());
								continue;
							}
						}
					}

				} else {
					removeCard(cb);
					return false;
				}
			} else if ((hand.get(3).size() - 2) % 3 == 0) {
				if (hand.get(1).size() % 3 == 0 && hand.get(2).size() % 3 == 0 && hand.get(0).size() % 3 == 0) {
					for (int i = 0; i < hand.get(3).size() - 1; i++) {
						if (hand.get(3).get(i) == hand.get(3).get(i + 1)) {
							dbCard = hand.get(3).get(i);
							hand.get(3).remove((Integer) dbCard);
							hand.get(3).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(3).add(dbCard);
								hand.get(3).add(dbCard);
								hand.get(3).sort(new XcmjCardComparator());
								return true;
							} else {
								hand.get(3).add(dbCard);
								hand.get(3).add(dbCard);
								hand.get(3).sort(new XcmjCardComparator());
								continue;
							}
						}
					}

				} else {
					return false;
				}
			}
			return false;
		} else {
			String card = cb.getType() + String.valueOf(cb.getNumber());
			setTypeNum();
			if (!pgArea.getPArea().contains(card))
				if (isGang(cb)) {
					removeCard(cb);
					removeCard(cb);
					removeCard(cb);
					setTypeNum();
					addCard(cb);
					addCard(cb);
					addCard(cb);
				} else if (isPeng(cb)) {
					removeCard(cb);
					removeCard(cb);
					setTypeNum();
					addCard(cb);
					addCard(cb);
				}

			if (typeNum == 3) {
				System.out.println("玩家" + getPosition() + "花色数为" + typeNum + "   尚未满足条件");
				return false;
			}
			System.out.println("玩家" + getPosition() + "花色数为" + typeNum + "   满足第一个条件");
			addCard(cb);
			// ciow
			if ((hand.get(0).size() - 2) % 3 == 0) {
				if (hand.get(1).size() % 3 == 0 && hand.get(2).size() % 3 == 0 && hand.get(3).size() % 3 == 0) {
					for (int i = 0; i < hand.get(0).size() - 1; i++) {
						if (hand.get(0).get(i) == hand.get(0).get(i + 1)) {
							dbCard = hand.get(0).get(i);
							hand.get(0).remove((Integer) dbCard);
							hand.get(0).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(0).add(dbCard);
								hand.get(0).add(dbCard);
								hand.get(0).sort(new XcmjCardComparator());
								removeCard(cb);
								return true;
							} else {
								hand.get(0).add(dbCard);
								hand.get(0).add(dbCard);
								hand.get(0).sort(new XcmjCardComparator());
								continue;
							}
						}
					}
				} else {
					removeCard(cb);
					return false;
				}
			} else if ((hand.get(1).size() - 2) % 3 == 1) {
				if (hand.get(0).size() % 3 == 0 && hand.get(2).size() % 3 == 0 && hand.get(3).size() % 3 == 0) {
					for (int i = 0; i < hand.get(1).size() - 1; i++) {
						if (hand.get(1).get(i) == hand.get(1).get(i + 1)) {
							dbCard = hand.get(1).get(i);
							hand.get(1).remove((Integer) dbCard);
							hand.get(1).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(1).add(dbCard);
								hand.get(1).add(dbCard);
								hand.get(1).sort(new XcmjCardComparator());
								removeCard(cb);
								return true;
							} else {
								hand.get(1).add(dbCard);
								hand.get(1).add(dbCard);
								hand.get(1).sort(new XcmjCardComparator());
								continue;
							}
						}
					}

				} else {
					removeCard(cb);
					return false;
				}
			} else if ((hand.get(2).size() - 2) % 3 == 0) {
				if (hand.get(1).size() % 3 == 0 && hand.get(0).size() % 3 == 0 && hand.get(3).size() % 3 == 0) {
					for (int i = 0; i < hand.get(2).size() - 1; i++) {
						if (hand.get(2).get(i) == hand.get(2).get(i + 1)) {
							dbCard = hand.get(2).get(i);
							hand.get(2).remove((Integer) dbCard);
							hand.get(2).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(2).add(dbCard);
								hand.get(2).add(dbCard);
								hand.get(2).sort(new XcmjCardComparator());
								removeCard(cb);
								return true;
							} else {
								hand.get(2).add(dbCard);
								hand.get(2).add(dbCard);
								hand.get(2).sort(new XcmjCardComparator());
								continue;
							}
						}
					}

				} else {
					removeCard(cb);
					return false;
				}
			} else if ((hand.get(3).size() - 2) % 3 == 0) {
				if (hand.get(1).size() % 3 == 0 && hand.get(2).size() % 3 == 0 && hand.get(0).size() % 3 == 0) {
					for (int i = 0; i < hand.get(3).size() - 1; i++) {
						if (hand.get(3).get(i) == hand.get(3).get(i + 1)) {
							dbCard = hand.get(3).get(i);
							hand.get(3).remove((Integer) dbCard);
							hand.get(3).remove((Integer) dbCard);
							if (isThree(hand.get(0)) && isThree(hand.get(1)) && isThree(hand.get(2))
									&& isThree(hand.get(3))) {
								hand.get(3).add(dbCard);
								hand.get(3).add(dbCard);
								hand.get(3).sort(new XcmjCardComparator());
								removeCard(cb);
								return true;
							} else {
								hand.get(3).add(dbCard);
								hand.get(3).add(dbCard);
								hand.get(3).sort(new XcmjCardComparator());
								continue;
							}
						}
					}

				} else {
					removeCard(cb);
					return false;
				}
			}
			removeCard(cb);
			return false;
		}
	}

	// 摸牌
	public XcmjCardBean getCard() {
		// 记录当前轮到的玩家
		xcmjGame.currentPlayer = position;
		// 从牌堆中拿牌
		XcmjCardBean cardBean = xcmjGame.cardPile.getCard();

		cardBean.setUserId(userId);
		cardBean.setOwner(position);
		// cb.setTime();
		addCard(cardBean);

		// 通知玩家摸到的牌
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result = true;
		// 当前轮到的玩家
		outJson.oper = xcmjGame.currentPlayer;
		// 玩家的Id
		outJson.userId = userId;
		// 手牌数量
		outJson.cardsNum = cards.size();
		// 摸到的牌信息
		outJson.cb = cardBean;
		// 设置结果码，他们收到的结果
		res.setResultCode(ProtocalRegulation.OTHERSGETCARD);
		String data = GsonUtils.beanToJson(outJson);
		System.out.println(data);
		res.setContent(data);
		xcmjGame.sendToOthers(res, position);

		// outJson.cb = cb;
		// 设置结果码，自己收到的结果
		res.setResultCode(ProtocalRegulation.SELFGETCARD);
		data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		session.write(res);

		return cardBean;
	}

	// public void disCard(ICardBean icb) {
	// XcmjCardBean cb = (XcmjCardBean) icb;
	// removeCard(cb);
	// g.dp.discard(cb);
	// }

	public boolean isBanker() {
		// TODO Auto-generated method stub
		return banker;
	}

	public void setBanker(boolean s) {
		// TODO Auto-generated method stub
		this.banker = s;
	}

	// 碰杠胡检测
	public boolean listenHPG(XcmjCardBean cardBean) {
		xcmjGame.lastCb = cardBean;
		// if (!(gang = isGang(cardBean))) {
		// if (cardBean.getOwner() != number) {
		// peng = isPeng(cardBean);
		// }
		// }
		boolean canGang = canGang(cardBean);
		boolean canPeng = canPeng(cardBean);

		// hu = isHu(cardBean);
		// // 通知可以进行碰杠胡
		// JMessageProtocalRes res = new JMessageProtocalRes();
		// StandardFormat outJson = new StandardFormat();
		// outJson.result = true;
		// outJson.userId = userId;
		// outJson.peng = peng;
		// outJson.gang = gang;
		// outJson.hu = hu;
		// res.setResultCode(ProtocalRegulation.OPERATION);
		// String data = GsonUtils.beanToJson(outJson);
		// res.setContent(data);
		// session.write(res);

		if (canGang || canPeng) {
			return true;
		} else {
			return false;
		}

		// if (hu || peng || gang) {
		// hu = peng = gang = false;
		// return true;
		// } else
		// return false;
	}

	// 别人打过来是否能杠
	public boolean canGang(XcmjCardBean cardBean) {
		int type = cardBean.getTypeInt();
		int number = cardBean.getNumber();

		int sumOfCard = 0;
		for (int i = 0; i < handCards.get(type - 1).size(); i++) {
			if (handCards.get(type - 1).get(i).getNumber() == number
					&& handCards.get(type - 1).get(i).getTypeInt() == type) {
				sumOfCard++;
			}
		}
		if (sumOfCard == 3)
			return true;
		else
			return false;
	}

	// 别人打过来是否能碰
	public boolean canPeng(XcmjCardBean cardBean) {
		int type = cardBean.getTypeInt();
		int number = cardBean.getNumber();

		int sumOfCard = 0;
		for (int i = 0; i < handCards.get(type - 1).size(); i++) {
			if (handCards.get(type - 1).get(i).getNumber() == number
					&& handCards.get(type - 1).get(i).getTypeInt() == type) {
				sumOfCard++;
			}
		}
		if (sumOfCard == 2)
			return true;
		else
			return false;
	}

	// 出牌，客户端发json过来，调用
	public void doPass(StandardFormat inJson, IoSession session) {
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result = true;
		outJson.resultInfo = "过成功";
		// 自己出牌收到反馈
		res.setResultCode(ProtocalRegulation.PASSSUCCESS);
		String data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		session.write(res);
		
		xcmjGame.players[(xcmjGame.currentPlayer + 1) % 4].getCard();
	}

	// 出牌，客户端发json过来，调用
	public void doDiscard(StandardFormat inJson, IoSession session) {
		// TODO Auto-generated method stub
		// setDraw(true);
		xcmjGame.currentPlayer = position;
		xcmjGame.opNum = 0;
		xcmjGame.aOpNum = 0;
		// 清空操作缓存
		xcmjGame.operations[0] = null;
		xcmjGame.operations[1] = null;
		xcmjGame.operations[2] = null;
		xcmjGame.operations[3] = null;

		XcmjCardBean disCardBean = new XcmjCardBean();
		disCardBean.setTypeInt((int) inJson.cardType);
		disCardBean.setNumber((int) inJson.cardNumber);
		disCardBean.setIndex((int) inJson.cardIndex);
		disCardBean.setUserId((String) inJson.userId);

		disCardBean.setName();

		System.out.println("打出的牌为：");
		System.out.println(disCardBean.getName());

		removeCard(disCardBean);
		// 出牌
		sendCards.add(disCardBean);
		// xcmjGame.discardPile.discard(disCardBean);
		//
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result = true;
		outJson.oper = position;
		outJson.userId = userId;
		outJson.cb = disCardBean;
		// 出牌后，其他玩家收到的消息
		res.setResultCode(ProtocalRegulation.OTHERSDISCARD);

		String data = GsonUtils.beanToJson(outJson);
		System.out.println(data);
		// 设置消息内容
		res.setContent(data);
		// 发送其他玩家消息
		xcmjGame.sendToOthers(res, position);
		// 检测其他三个玩家的胡碰杠

		for (int i = 1; i < 4; i++) {
			if (xcmjGame.players[(position + i) % 4].listenHPG(disCardBean)) {
				// 需要等待的操作总数
				xcmjGame.opNum++;
			}
		}

		res = new JMessageProtocalRes();
		outJson = new StandardFormat();
		outJson.result = true;
		outJson.resultInfo = "出牌成功";
		// 自己出牌收到反馈
		res.setResultCode(ProtocalRegulation.SELFDISCARD);
		data = GsonUtils.beanToJson(outJson);
		res.setContent(data);
		session.write(res);
		if (xcmjGame.opNum == 0)
			xcmjGame.players[(position + 1) % 4].getCard();
		xcmjGame.printAllPlayer();
	}

	public int getZimoTimes() {
		return zimoTimes;
	}

	public void setZimoTimes(int zimoTimes) {
		this.zimoTimes = zimoTimes;
	}

	public int getHuTimes() {
		return huTimes;
	}

	public void setHuTimes(int huTimes) {
		this.huTimes = huTimes;
	}

	public int getMingGangTimes() {
		return mingGangTimes;
	}

	public void setMingGangTimes(int mingGangTimes) {
		this.mingGangTimes = mingGangTimes;
	}

	public int getAnGangTimes() {
		return anGangTimes;
	}

	public void setAnGangTimes(int anGangTimes) {
		this.anGangTimes = anGangTimes;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public int getJiePaoTimes() {
		return jiePaoTimes;
	}

	public void setJiePaoTimes(int jiePaoTimes) {
		this.jiePaoTimes = jiePaoTimes;
	}

	public int getDianPaoTimes() {
		return dianPaoTimes;
	}

	public void setDianPaoTimes(int dianPaoTimes) {
		this.dianPaoTimes = dianPaoTimes;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public IoSession getSession() {
		return session;
	}

	public void setSession(IoSession session) {
		this.session = session;
	}

	
}
